package com.coocaa.launcher;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.coocaa.x.uipackage.ThemeActivity;
import com.tianci.appstore.R;

/**
 * Created by luwei on 15-7-8.
 */
public class LauncherBaseActivity extends ThemeActivity {

    private ImageView loadingView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    public void showLoading() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (loadingView == null) {
                    loadingView = new ImageView(LauncherBaseActivity.this);
                    loadingView.setLayoutParams(new FrameLayout.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER));
                    loadingView.setBackgroundResource(R.drawable.launcher_loading);
                }
                if (loadingView.getParent() == null) {
                    getRootViewGroup().addView(loadingView);
                    loadingView.setVisibility(View.VISIBLE);
                    AnimationDrawable anim = (AnimationDrawable) loadingView.getBackground();
                    anim.start();
                }
            }
        });

    }

    public void hideLoading() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (loadingView != null) {
                    AnimationDrawable anim = (AnimationDrawable) loadingView.getBackground();
                    anim.stop();
                    loadingView.setVisibility(View.GONE);
                    getRootViewGroup().removeView(loadingView);
                }
            }
        });

    }

    private ViewGroup getRootViewGroup() {
        return (ViewGroup) ((ViewGroup) findViewById(android.R.id.content))
                .getChildAt(0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    private static final String APP_SESSION_KEY = "55a9c5cc67e58e171500081c";

    @Override
    protected String getAppSessionKey() {
        return APP_SESSION_KEY;
    }
}
